|
|
|
|
|
|
|
Managing Security with the Session Object |
|
|
|
|
|
|
|
|
As alluded to earlier, IIS has features that allow your ASP application to retain information between pages. You can, for example, have a form where the user selects a country from a list and then submits that value to an ASP page. The ASP page can store this value on the server so it is available to other ASP pages. This is accomplished through the use of Session variables. Session variables are declared and accessed like a collection. To create a session variable, simply give it a name and value: |
|
|
|
|
|
|
|
|
Session("Country")="United States" |
|
|
|
|
|
|
|
|
One frequent use of ASP session variables is to manage security. When database information is on the Internet, you probably do not want everyone in the world to be able to update it. Additionally, you might want to restrict access so that only certain people can see certain information. If you want to add security to your Web site, the obvious solution is to display some type of login page so that only valid users can get into your site. Your login page can be a simple HTML form with fields for user name and password. The form submits these values to an ASP file that checks the user name and password for validity. |
|
|
|
|
|
|
|
|
But even if you add such a login page, how do you prevent users from simply entering the URL they want and bypassing the login screen? The answer is to use session variables. Suppose you have an ASP file, REPORT.ASP, that you want to secure. You can add a simple If statement at the top of the page to verify that a session variable has been set: |
|
|
|
|
|
|
|
|
<%
If Session("UserName")="" Then
Response.Write "You are not logged in!<BR>"
Response.End
End If
Response.Write ("Welcome, " & Session("UserName"))
%> |
|
|
|
|
|
|
|
|
The If statement checks the contents of a session variable called UserName and ends the response from the Web server if the value is empty. In other words, the contents of REPORT.ASP after the If statement are available only to users who have the session variable UserName set to a value. |
|
|
|
|
| |
|
|
|
|
If you have multiple pages that you want to secure, use an include file to make the security check easier to maintain. |
|
|
|
|
|
|
|
|
|
|
To validate a user login and set the UserName session variable, you need to write some ASP code. This code, shown in Listing 31.6, checks the information from the login form and sets the session variable if the user's password is correct. |
|
|
|
|
|